$(function(){
	exit();
})
function exit(){
	$("#firmId").val("");
	$("#firmNames").val("");
	$("#firmAddress").val("");
	$("#isva").val("");
	$("#oper").val("");
	load();
	//关闭左右页面层
	layer.closeAll('page');
}
layui.use(['form', 'layedit', 'laydate','table','element', 'tree', 'layer', 'form', 'upload'], function(){
  var table = layui.table
  ,laydate = layui.laydate
  ,form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,layer = layui.layer
  ,$ = layui.jquery
  ,tree = layui.tree;
  
  //监听工具条
  table.on('tool(demo)', function(obj){ 
    var da = obj.data;
    
    if(obj.event === 'edit'){
    	var url="../../firm/findById.action";
        var data={"firmId":da.firmId};
        $.post(url,data,function(mes){
        	layer.open({
    	        type: 1	 //此处以iframe举例
    	        ,title: '数据修改'
    	        ,area: ['700px', '450px']
        		,offset: 'auto'
    	        ,content:$('#addOrUpdate')
    	        ,success: function(layero){
    	        	$("#firmId").val(mes.firmId);
    	        	$("#firmNames").val(mes.firmName);
    	        	$("#firmAddress").val(mes.firmAddress);  
    	        	$("#oper").val(mes.oper);
    	        	$("#isva").val(mes.isva);
    	        	//渲染
    	        	load();
        		},end : function() {
        			$("#oper").attr({"disabled":false});		 
    	        	//弹框关闭的回调函数
    				$("#addOrUpdate").hide();
    				exit();
    			}
    	    });
        	
        },"json");
    }else if(obj.event === 'del'){
        layer.confirm('真的删除行么', function(index){
        	var url="../../firm/delete.action";
        	var data={"firmId":da.firmId}
        	$.post(url,data,function(mes){
        		if(mes==1){
        			obj.del();
                    layer.close(index);
                    layer.msg("删除成功");
        		}else{
        			layer.msg("删除失败");
        		}
        	},"json");
            
          });
        }
  });
  
  
  var $ = layui.$, active = {
		    reload: function(){
		      var typeName = $('#firmName');
		      //执行重载
		      table.reload('idTest', {
		        page: {
		          curr: 1 //重新从第 1 页开始
		        }
		        ,where: {
		        	firmName:typeName.val()
		        }
		      });
		    }
		  };
		  
		  $('.demoTable .layui-btn').on('click', function(){
		    var type = $(this).data('type');
		    active[type] ? active[type].call(this) : '';
		  });
		  
		  $('#insert').on('click', function(){
			  layer.open({
	    	        type: 1	 //此处以iframe举例
	    	        ,title: '数据增加'
	    	        ,offset: 'auto'
	    	        ,area: ['700px', '450px']
	    	        ,content:$('#addOrUpdate')
	    	        ,end : function() {
	    	        	//弹框关闭的回调函数
	    				$("#addOrUpdate").hide();
	    				exit();
	    				$(".layui-laypage-btn")[0].click();//当前表格刷新
	    			}
	    	    });
		  });
		  $('#sub').on('click', function(){
			  var url="../../firm/addOrUpdate.action";
			  var data=$("#myform").serialize();
			  $.post(url,data,function(mes){
				  if(mes==1){
					  $("#addOrUpdate").hide();
					  parent.layer.msg('操作成功', {
					      icon: 1
					  });
					  $(".layui-laypage-btn")[0].click();//当前表格刷新
					  exit();
				  }else{
					  parent.layer.msg('操作失败', {
					      icon: 2
					  });
				  }
				  load();
			  },"json");
		  });
		  
});

function firmN(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#firmNames").val()==null || $("#firmNames").val()==''){
			 parent.layer.msg('名称不能为空', {
			      icon: 5,
			      anim :6
			  });
		}
	});
}

function firmA(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#firmAddress").val()==null || $("#firmAddress").val()==''){
			 parent.layer.msg('地址不能为空', {
			      icon: 5,
			      anim :6
			  });
			 
		}
	});
}

function op(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#oper").val()==null || $("#oper").val()==''){
			 parent.layer.msg('操作人不能为空', {
			      icon: 5,
			      anim :6
			  });
			 
		}
	});
}
function load() {
	 var form = layui.form
	 form.render();
}
